import auctionhaus.Listing
import auctionhaus.Customer
import auctionhaus.Bid
import groovy.time.TimeCategory
import grails.util.Environment
import auctionhaus.Role
import auctionhaus.CustomerRole

class BootStrap {

    def springSecurityService

    def init = { servletContext ->

        //IF RUNNING UNIT OR INTEGRATION TESTS RUN IN "DEV" MODE. THIS WILL DISABLE BOOTSTRAPPING
        //IF RUNNING MANUAL UI test-app THEN RUN IN "TEST" MODE. THIS WILL ENABLE BOOTSTRAPPING

        if(!Listing.count && Environment.current == Environment.TEST){
            
            println "Bootstrapping test data."

            println "Creating roles."
            
            def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true)
            def userRole = new Role(authority: 'ROLE_USER').save(flush: true)

            println "Creating users."
                        
            Customer administrator = new Customer(email: "admin@auctionhaus.com", password: "password", enabled:  true).save(flush: true)          
            Customer seller = new Customer(email:  "adambackstrom@gmail.com", password: "gibbrish", enabled: true).save(flush: true)
            Customer bidder1 = new Customer(email:  "kleczka@gmail.com", password: "password", enabled: true).save(flush: true)
            Customer bidder2 = new Customer(email:  "kleczka@yahoo.com", password: "password", enabled: true).save(flush: true)
            Customer bidder3 = new Customer(email:  "adam@adambackstrom.com", password: "password", enabled: true).save(flush: true)
            Customer mike1 = new Customer(email:  "mike@citronellasoftware.com", password: "password", enabled: true).save(flush: true)
            Customer mike2 = new Customer(email:  "mike@piragua.com", password:  "password", enabled: true).save(flush: true)

            println "Assigning roles."
            
            CustomerRole.create bidder1, adminRole, true
            CustomerRole.create seller, adminRole, true
            CustomerRole.create administrator, adminRole, true
            CustomerRole.create bidder2, userRole, true
            CustomerRole.create mike1, userRole, true
            CustomerRole.create mike2, userRole, true
            
            println "Creating listings."
            
            //first listing should not be listing since enddate is past
            def endDateTime = new Date()
            use(TimeCategory) {
                endDateTime = endDateTime + 30.second
            }

            Listing expiredListing = new Listing(seller: bidder1, name: "Expired Listing",
                    description: "This is an uber-sweet item.", endDateTime: endDateTime, startingBidPrice: 1).save(flush: true)

            Bid winningBid = new Bid(amount: 1500, bidDateTime:  new Date(), bidder: bidder2, listing: expiredListing)
            expiredListing.addToBids(winningBid)
            expiredListing.save(flush: true)

            
            new Listing(seller: seller, name: "1 AuctionHaus Listing",
                    description: "First test listing", endDateTime: new Date()+10, startingBidPrice: 100 ).save(flush: true)

            new Listing(seller: seller, name: "2 AuctionHaus Listing",
                    description: "Second test listing", endDateTime: new Date()+20, startingBidPrice: 200 ).save(flush: true)

            new Listing(seller: seller, name: "3 AuctionHaus Listing",
                    description: "Third test listing", endDateTime: new Date()+30, startingBidPrice: 300 ).save(flush: true)

            new Listing(seller: seller, name: "4 AuctionHaus Listing",
                    description: "Fourth test listing", endDateTime: new Date()+40, startingBidPrice: 400 ).save(flush: true)

            new Listing(seller: bidder2, name: "5 AuctionHaus Listing",
                    description: "Fifth test listing", endDateTime: new Date()+50, startingBidPrice: 500 ).save(flush: true)

            new Listing(seller: seller, name: "6 AuctionHaus Listing",
                    description: "Sixth test listing", endDateTime: new Date()+60, startingBidPrice: 600 ).save(flush: true)

            new Listing(seller: seller, name: "7 AuctionHaus Listing",
                    description: "Seventh test listing", endDateTime: new Date()+60, startingBidPrice: 600 ).save(flush: true)

            new Listing(seller: seller, name: "8 AuctionHaus Listing",
                    description: "Eigth test listing", endDateTime: new Date()+60, startingBidPrice: 600 ).save(flush: true)

            sleep(1000)

            new Listing(seller: bidder1, name: "9 AuctionHaus Listing",
                    description: "Ninth test listing", endDateTime: new Date()+60, startingBidPrice: 600 ).save(flush: true)

            sleep(1000)

            new Listing(seller: bidder2, name: "10 AuctionHaus Listing",
                    endDateTime: new Date()+60, startingBidPrice: 600 ).save(flush: true)

            sleep(1000)

            println "Creating bids."

            Listing listing11 = new Listing(seller: seller, name: "11 AuctionHaus Listing",
                    description: "Eleventh test listing", endDateTime: new Date()+60, startingBidPrice: 600 ).save(flush: true)
            Bid bid0 = new Bid(amount: 1500, bidDateTime:  new Date(), bidder: bidder1, listing: listing11)
            listing11.addToBids(bid0)
            listing11.save(flush: true)
                     
            sleep(1000)

            Listing listing12 = new Listing(seller: seller, name: "12 AuctionHaus Listing",
                    description: "Twelvth test listing", endDateTime: new Date()+60, startingBidPrice: 600 ).save(flush: true)

            Bid bid1 = new Bid(amount: 1000, bidDateTime: new Date(), bidder: bidder1, listing: listing12)
            listing12.addToBids(bid1)
            listing12.save(flush: true)
           
            sleep(1000)

            Bid bid2 = new Bid(amount: 2000, bidDateTime: new Date(), bidder:  bidder2, listing: listing12)
            listing12.addToBids(bid2)
            listing12.save(flush: true)
        }
    }
    def destroy = {
    }
}
